<html>
<head>
    <script src="../OLLoader.js"></script>
    <script type="text/javascript">

    function test_read_SOSGetObservation(t) {
        t.plan(13);

        var parser = new OpenLayers.Format.SOSGetObservation();
        var text =
            '<?xml version="1.0" encoding="UTF-8"?>' +
            '<om:ObservationCollection xmlns:om="http://www.opengis.net/om/1.0" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sa="http://www.opengis.net/sampling/1.0" gml:id="oc_0" xsi:schemaLocation="http://www.opengis.net/om/1.0 http://schemas.opengis.net/om/1.0.0/om.xsd http://www.opengis.net/sampling/1.0 http://schemas.opengis.net/sampling/1.0.0/sampling.xsd">' +
            '<gml:boundedBy>' +
            '<gml:Envelope srsName="urn:ogc:def:crs:EPSG:4326">' +
            '<gml:lowerCorner>52.1524 5.3722</gml:lowerCorner>' +
            '<gml:upperCorner>52.1524 5.3722</gml:upperCorner>' +
            '</gml:Envelope>' +
            '</gml:boundedBy>' +
            '<om:member>' +
            '<om:Measurement gml:id="o_51082">' +
            '<om:samplingTime>' +
            '<gml:TimeInstant xsi:type="gml:TimeInstantType">' +
            '<gml:timePosition>2009-12-02T10:35:00.000+01:00</gml:timePosition>' +
            '</gml:TimeInstant>' +
            '</om:samplingTime>' +
            '<om:procedure xlink:href="urn:ogc:object:feature:OSIRIS-HWS:4fc335bc-06d7-4d5e-a72a-1ac73b9f3b56"/>' +
            '<om:observedProperty xlink:href="urn:x-ogc:def:property:OGC::Temperature"/>' +
            '<om:featureOfInterest>' +
            '<sa:SamplingPoint gml:id="urn:ogc:object:feature:OSIRIS-HWS:4fc335bc-06d7-4d5e-a72a-1ac73b9f3b56">' +
            '<gml:name>Roof of the IfGI</gml:name>' +
            '<sa:position>' +
            '<gml:Point>' +
            '<gml:pos srsName="urn:ogc:def:crs:EPSG:4326">52.1524 5.3722</gml:pos>' +
            '</gml:Point>' +
            '</sa:position>' +
            '</sa:SamplingPoint>' +
            '</om:featureOfInterest>' +
            '<om:result uom="Cel">4.9</om:result>' +
            '</om:Measurement>' +
            '</om:member>' +
            '</om:ObservationCollection>';

        var res = parser.read(text);
        t.eq(res.measurements.length, 1, "One measurement parsed");
        t.eq(res.id, "oc_0", "Observation collection id correctly parsed");
        var measurement = res.measurements[0];
        t.eq(measurement.observedProperty, "urn:x-ogc:def:property:OGC::Temperature", "Observed property correctly parsed");
        t.eq(measurement.procedure, "urn:ogc:object:feature:OSIRIS-HWS:4fc335bc-06d7-4d5e-a72a-1ac73b9f3b56", "Procedure correctly parsed");
        t.eq(measurement.result.uom, "Cel", "Units of measurement correctly parsed");
        t.eq(measurement.result.value, "4.9", "Value correctly parsed");
        t.eq(measurement.samplingTime.timeInstant.timePosition, "2009-12-02T10:35:00.000+01:00", "Sampling time correctly parsed");

        var response = [];
        response.push('<?xml version="1.0" encoding="UTF-8"?>',
'<om:ObservationCollection gml:id="oc_0" xsi:schemaLocation="http://www.opengis.net/om/1.0 http://schemas.opengis.net/om/1.0.0/om.xsd http://www.opengis.net/sampling/1.0 http://schemas.opengis.net/sampling/1.0.0/sampling.xsd" xmlns:om="http://www.opengis.net/om/1.0" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:swe="http://www.opengis.net/swe/1.0.1" xmlns:sa="http://www.opengis.net/sampling/1.0">',
'  <gml:boundedBy>',
'    <gml:Envelope srsName="urn:ogc:def:crs:EPSG:4326">',
'      <gml:lowerCorner>46.611644 7.6103</gml:lowerCorner>',
'      <gml:upperCorner>51.9412 13.883498</gml:upperCorner>',
'    </gml:Envelope>',
'  </gml:boundedBy>',
'  <om:member>',
'    <om:Observation gml:id="ot_583227">',
'      <om:samplingTime>',
'        <gml:TimePeriod xsi:type="gml:TimePeriodType">',
'          <gml:beginPosition>2009-09-28T13:45:00.000+02:00</gml:beginPosition>',
'          <gml:endPosition>2009-09-28T13:45:00.000+02:00</gml:endPosition>',
'        </gml:TimePeriod>',
'      </om:samplingTime>',
'      <om:procedure xlink:href="urn:ogc:object:feature:OSIRIS-HWS:efeb807b-bd24-4128-a920-f6729bcdd111"/>',
'      <om:observedProperty>',
'        <swe:CompositePhenomenon gml:id="cpid0" dimension="1">',
'          <gml:name>resultComponents</gml:name>',
'          <swe:component xlink:href="urn:ogc:data:time:iso8601"/>',
'          <swe:component xlink:href="urn:ogc:def:property:OGC::Precipitation1Hour"/>',
'        </swe:CompositePhenomenon>',
'      </om:observedProperty>',
'      <om:featureOfInterest>',
'        <gml:FeatureCollection>',
'          <gml:featureMember>',
'            <sa:SamplingPoint gml:id="urn:ogc:object:feature:OSIRIS-HWS:efeb807b-bd24-4128-a920-f6729bcdd111" xsi:schemaLocation=" http://www.opengis.net/sampling/1.0 http://schemas.opengis.net/sampling/1.0.0/sampling.xsd">',
'              <gml:name>waether @ roof of the FH Kaernten, Villach, Austria</gml:name>',
'              <sa:sampledFeature xlink:href="urn:ogc:def:nil:OGC:unknown"/>',
'              <sa:position>',
'                <gml:Point>',
'                  <gml:pos srsName="urn:ogc:def:crs:EPSG:4326">46.611644 13.883498</gml:pos>',
'                </gml:Point>',
'              </sa:position>',
'            </sa:SamplingPoint>',
'          </gml:featureMember>',
'        </gml:FeatureCollection>',
'      </om:featureOfInterest>',
'      <om:result>',
'        <swe:DataArray>',
'          <swe:elementCount>',
'            <swe:Count>',
'              <swe:value>1</swe:value>',
'            </swe:Count>',
'          </swe:elementCount>',
'          <swe:elementType name="Components">',
'            <swe:DataRecord>',
'              <swe:field name="Time">',
'                <swe:Time definition="urn:ogc:data:time:iso8601"/>',
'              </swe:field>',
'              <swe:field name="feature">',
'                <swe:Text definition="urn:ogc:data:feature"/>',
'              </swe:field>',
'              <swe:field name="urn:ogc:def:property:OGC::Precipitation1Hour">',
'                <swe:Quantity definition="urn:ogc:def:property:OGC::Precipitation1Hour">',
'                  <swe:uom code="mm"/>',
'                </swe:Quantity>',
'              </swe:field>',
'            </swe:DataRecord>',
'          </swe:elementType>',
'          <swe:encoding>',
'            <swe:TextBlock decimalSeparator="." tokenSeparator="," blockSeparator=";"/>',
'          </swe:encoding>',
'          <swe:values>2009-09-28T13:45:00.000+02:00,urn:ogc:object:feature:OSIRIS-HWS:efeb807b-bd24-4128-a920-f6729bcdd111,0.0;</swe:values>',
'        </swe:DataArray>',
'      </om:result>',
'    </om:Observation>',
'  </om:member>',
'</om:ObservationCollection>');
        text = response.join("");
        var res = parser.read(text);
        t.eq(res.observations.length, 1, "1 observation parsed");
        var observation = res.observations[0];
        t.eq(observation.procedure, "urn:ogc:object:feature:OSIRIS-HWS:efeb807b-bd24-4128-a920-f6729bcdd111", "procedure parsed correctly");
        t.eq(observation.fois.length, 1, "One foi parsed for the observation");
        var foi = observation.fois[0];
        var feature = foi.features[0];
        t.eq(feature.attributes.id, "urn:ogc:object:feature:OSIRIS-HWS:efeb807b-bd24-4128-a920-f6729bcdd111", "Foi id correctly parsed");
        t.eq(feature.attributes.name, "waether @ roof of the FH Kaernten, Villach, Austria", "Foi name correctly parsed");
        t.ok(feature.geometry instanceof OpenLayers.Geometry.Point, "Geometry correctly parsed");
    }

    function test_write_SOSGetObservation(t) {
        t.plan(2);
        var expect = '<GetObservation xmlns="http://www.opengis.net/sos/1.0" version="1.0.0" service="SOS" xsi:schemaLocation="http://www.opengis.net/sos/1.0 http://schemas.opengis.net/sos/1.0.0/sosGetObservation.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><offering>TEMPERATURE</offering><eventTime><ogc:TM_Equals xmlns:ogc="http://www.opengis.net/ogc"><ogc:PropertyName>urn:ogc:data:time:iso8601</ogc:PropertyName><gml:TimeInstant xmlns:gml="http://www.opengis.net/gml"><gml:timePosition>latest</gml:timePosition></gml:TimeInstant></ogc:TM_Equals></eventTime><procedure>urn:ogc:object:feature:OSIRIS-HWS:4fc335bc-06d7-4d5e-a72a-1ac73b9f3b56</procedure><observedProperty>urn:x-ogc:def:property:OGC::Temperature</observedProperty><responseFormat>text/xml;subtype="om/1.0.0"</responseFormat><resultModel>Measurement</resultModel><responseMode>inline</responseMode></GetObservation>';
        var format = new OpenLayers.Format.SOSGetObservation();
        var output = format.write({eventTime: 'latest', resultModel: 'Measurement', responseMode: 'inline',
            procedures: ['urn:ogc:object:feature:OSIRIS-HWS:4fc335bc-06d7-4d5e-a72a-1ac73b9f3b56'], responseFormat: 'text/xml;subtype="om/1.0.0"',
            offering: 'TEMPERATURE', observedProperties: ['urn:x-ogc:def:property:OGC::Temperature']});
        t.xml_eq(output, expect, "Request XML is written out correctly");

        var expected = [];

        expected.push('<?xml version="1.0" encoding="UTF-8"?>',
'<GetObservation xmlns="http://www.opengis.net/sos/1.0"',
'  xmlns:gml="http://www.opengis.net/gml"',
'  xmlns:om="http://www.opengis.net/om/1.0"',
'  xmlns:ogc="http://www.opengis.net/ogc"',
'  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"',
'  xsi:schemaLocation="http://www.opengis.net/sos/1.0 http://schemas.opengis.net/sos/1.0.0/sosGetObservation.xsd"',
'  service="SOS" version="1.0.0">',
'  <offering>RAIN_GAUGE</offering>',
'  <eventTime>',
'    <ogc:TM_Equals>',
'      <ogc:PropertyName>urn:ogc:data:time:iso8601</ogc:PropertyName>',
'      <gml:TimeInstant>',
'        <gml:timePosition>latest</gml:timePosition>',
'      </gml:TimeInstant>',
'    </ogc:TM_Equals>',
'  </eventTime>',
'  <observedProperty>urn:ogc:def:property:OGC::Precipitation1Hour</observedProperty>',
'  <featureOfInterest>',
'    <ObjectID>urn:ogc:object:feature:OSIRIS-HWS:3d3b239f-7696-4864-9d07-15447eae2b93</ObjectID>',
'  </featureOfInterest>',
'  <responseFormat>text/xml;subtype="om/1.0.0"</responseFormat>',
'</GetObservation>');
        expect = expected.join("");
        var output = format.write({eventTime: 'latest', offering: 'RAIN_GAUGE',
            observedProperties: ['urn:ogc:def:property:OGC::Precipitation1Hour'],
            responseFormat: 'text/xml;subtype="om/1.0.0"',
            foi: {objectId: 'urn:ogc:object:feature:OSIRIS-HWS:3d3b239f-7696-4864-9d07-15447eae2b93'}});
        t.xml_eq(output, expect, "Request XML is written out correctly");
    }

    </script>
</head>
<body>
</body>
</html>
